The present invention relates to the checking of units and dimensional homogeneity of expressions, and in particular to checking of the correctness and consistency of units and dimensions of variables and constants used in expressions, such as those used in computer programs. The present invention relates particularly to a method and apparatus for performing such checking.
Algebraic expressions are typically used in computer programs to assign values to variables. These algebraic expressions may include operations with various measurement units and dimensions. Examples of such units and dimensions include meters, seconds, cubic meters, inches, kilograms, meters per second, and many more.
Prior methods of software testing of computer programs do not check for correctness and consistency of units and dimensions of variables and constants used in expressions. A failure to correctly do so may lead to serious miscalculations. A simple example of this would be if an algebraic expression containing a variable or constant having units of metres per second is added to another variable or constant with units of centimetres per second or inches per second.
According to the article xe2x80x9cNASA Reworks its Sums after Mars Fiascoxe2x80x9d by Reichhardt, T., published in Nature, Vol. 401, Oct. 7, 1999, p. 517, the loss of NASA""s Mars Climate Orbitor on Sep. 23, 1999, as it was about to enter orbit, has been attributed to a confusion between imperial and metric units.
It is an object of the present invention to provide a method of checking units and dimensional homogeneity of expressions.
According to a first aspect of the invention, there is provided a method of checking a computer program for unit or dimensional homogeneity, the computer program including expressions, and the method comprising the steps of:
accepting as input corresponding strings for variables used in the expressions;
identifying and substituting instances of the variables with the corresponding strings;
recasting the expressions into a form of one or more token pairs arranged sequentially in a string, each said token pair comprising an operator followed by an operand;
reducing the strings in accordance with a set of predetermined simplifying rules; and
checking the reduced strings for unit or dimensional homogeneity in accordance with a set of predetermined conditions.
The computer program may include assignment statements where the right hand sides of the assignment statements constitute expressions.
The computer program may also include functions, in which case the method comprises the further steps of checking for unit or dimensional homogeneity of the functions by:
accepting as input corresponding strings for the functions and function parameters used in the expressions;
creating an assignment statement for each of the function parameters;
identifying and substituting instances of variables on right hand side of the assignment statements with the corresponding strings;
substituting the function parameters with the corresponding strings;
recasting sides of the assignment statements into the form of one or more token pairs arranged sequentially in a string;
reducing the strings in accordance with the set of predetermined simplifying rules; and
checking the reduced strings for unit or dimensional homogeneity in accordance with the set of predetermined conditions.
Preferably, the simplifying rules comprise performing the steps of:
arranging token pairs into subgroups;
arranging operand tokens in an arranged subgroup in order;
reducing the ordered operands by consolidating one or more constants and eliminating variables of opposite effect to form reduced subgroups; and
consolidating one or more multiple instances of similar subgroups, to produce a reduced string.
Preferably, the set of predetermined conditions for expressions comprise performing the step of:
testing whether the reduced string reduces to a single term; or
testing whether the reduced string reduces to the zero string.
The set of predetermined conditions for assignment statements preferably comprise performing the steps of:
testing whether the reduced string reduces to a single term; or
testing whether the reduced string reduces to the zero string; and
testing by comparison whether the sides of the assignment statements have corresponding unit-groups.
According to a further aspect of the invention, there is provided an apparatus for checking a computer program for unit or dimensional homogeneity, the computer program including expressions, the apparatus comprising:
means for accepting as input corresponding strings for variables used in the expressions;
means for identifying and substituting instances of the variables with the corresponding strings;
means for recasting the expressions into a form of one or more token pairs arranged sequentially in a string, each said token pair comprising an operator followed by an operand;
means for reducing the strings in accordance with a set of predetermined simplifying rules; and
means for checking the reduced strings for unit or dimensional homogeneity in accordance with a set of predetermined conditions.
When the computer program includes functions, the apparatus comprising an additional means for checking for unit or dimensional homogeneity of the functions, this additional means comprising:
means for accepting as input corresponding strings for the functions and function parameters used in the expressions;
means for creating an assignment statement for each of the function parameters;
means for identifying and substituting instances of variables on right hand side of the assignment statements with the corresponding strings;
means for substituting the function parameters with the corresponding strings;
means for recasting sides of the assignment statements into the form of one or more token pairs arranged sequentially in a string;
means for reducing the strings in accordance with the set of predetermined simplifying rules; and
means for checking the reduced strings for unit or dimensional homogeneity in accordance with the set of predetermined conditions.